package com.ebuy.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ebuy.dao.CustomerDAO;
import com.ebuy.model.Customer;

public class RegistrationServlet extends BaseServlet {

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	private void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (!name.isEmpty() && !username.isEmpty() && !password.isEmpty()) {
			CustomerDAO dao = new CustomerDAO();

			Customer customer = new Customer();
			customer.setName(name);
			customer.setUsername(username);
			customer.setPassword(password);

			dao.createCustomer(customer);

			redirect(response, "", false);
		} else {
			redirect(response, "", false);
		}
	}

}
